package com.hxzy.tms.action;

import java.util.List;
import java.util.Map;

import com.hxzy.tms.ExtendsActionSupport;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ProjectService;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;
import com.opensymphony.xwork2.Action;

public class ProjectAction extends ExtendsActionSupport {
    private static final long serialVersionUID = 5646348887040282015L;
    private static final String PROJECT_KEY = "project";
    private Project pro;
    private Task task;
    private ProjectService proService;
    private TaskService taskService;

    public String index() throws Exception {
        return Action.INPUT;
    }

    public String create() throws Exception {
        User user = new User();
        /*
         * if (user==null) { setNextPage("login"); }
         */
        user.setId(1L);
        Team team = new Team();
        team.setId(1L);
        pro.setTeamId(team.getId());
        pro.setUidCreate(user);
        pro.setUidManager(user);
        pro.setUidModified(user.getId());
        pro.setGmtModified(getTimeNow().getTime());
        ServiceResult<Project> result = proService.createProject(pro);
        writeJson(result);
        return null;
    }

    public String list() throws Exception {
        return Action.SUCCESS;
    }

    public String user_list() throws Exception {
        User user = new User();
        user.setId(1L);
        List<Map<String, String>> projects = proService.showMyProject(user);
        writeJson(projects);
        return null;
    }

    public String pro_list() throws Exception {
        Team team = new Team();
        team.setId(1L);
        List<Project> projects = proService.showAllProject(team);
        writeJson(projects);
        return null;
    }

    public String change() throws Exception {
        User user = (User) getSession(LoginAction.SESSION_KEY);
        Project proOld = (Project) getSession(PROJECT_KEY);
        proOld.setDesc(pro.getDesc());
        proOld.setTitle(pro.getTitle());
        proOld.setUidModified(user.getId());
        proOld.setGmtModified(getTimeNow().getTime());
        return null;
    }

    public String post() throws Exception {
        writeJson(proService.getNowProject(pro.getId()));
        return Action.INPUT;
    }

    public Project getPro() {
        return pro;
    }

    public void setPro(Project pro) {
        this.pro = pro;
    }

    public ProjectService getProService() {
        return proService;
    }

    public void setProService(ProjectService proService) {
        this.proService = proService;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

}
